package com.example.asmdemo;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;

import java.util.ArrayList;
import java.util.List;

public class BallView extends View {
    private Paint mPaint;
    private Bitmap mBitmap;
    private List<Ball> mBalls = new ArrayList<>();
    private ValueAnimator mValueAnimator;
    private float d = 2;

    public BallView(Context context) {
        this(context, null);
    }

    public BallView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BallView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mValueAnimator = ObjectAnimator.ofFloat(0, 1);
        mValueAnimator.setDuration(2000);
        mValueAnimator.setRepeatCount(-1);
        mValueAnimator.setInterpolator(new LinearInterpolator());
        mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                updateBall();
                invalidate();
            }
        });

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.pic);
        for (int i = 0; i < mBitmap.getWidth(); i++) {
            for (int j = 0; j < mBitmap.getHeight(); j++) {
                Ball ball = new Ball();
                mBalls.add(ball);
                ball.color = mBitmap.getPixel(i, j);
                ball.dx = i * d + d / 2;
                ball.dy = j * d + d / 2;
                ball.radius = d / 2;

                //vx速度（-20,20）
                ball.vx = (float) (Math.pow(-1, Math.ceil(Math.random() * 1000)) * 20 * Math.random());
                ball.vy = rangeInt(-15, 35);
                //加速度
                ball.ax = 0.0f;
                ball.ay = 0.98f;

            }
        }
    }

    private void updateBall() {
        for (int i = 0; i < mBalls.size(); i++) {
            Ball ball = mBalls.get(i);
            ball.dx += ball.vx;
            ball.dy += ball.vy;

            ball.vx += ball.ax;
            ball.vy += ball.ay;
        }
    }


    private int rangeInt(int i, int j) {
        int max = Math.max(i, j);
        int min = Math.min(i, j) - 1;
        return (int) (min + Math.ceil(Math.random() * (max - min)));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(50, 50);
        for (int i = 0; i < mBalls.size(); i++) {
            Ball ball = mBalls.get(i);
            mPaint.setColor(ball.color);
            canvas.drawCircle(ball.dx, ball.dy, ball.radius, mPaint);
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            mValueAnimator.start();
        }
        return super.onTouchEvent(event);
    }
}
